package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.UserExamResultMapper;
import com.think.teaching.model.ExamResult;
import com.think.teaching.model.UserExamResult;
import com.think.teaching.service.UserExamResultService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户考试记录 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class UserExamResultServiceImpl extends BaseServiceImpl<UserExamResultMapper, UserExamResult> implements UserExamResultService {

	final
	ExamResultServiceImpl examResultService;

	public UserExamResultServiceImpl(ExamResultServiceImpl examResultService) {
		this.examResultService = examResultService;
	}

	@Override
	@Transactional
	public UserExamResult AddUserExamResult(UserExamResult userExamResult) {
		try{
		if (this.save(userExamResult)) {
			for (ExamResult examResult:userExamResult.getExamResults())
				examResult.setResultId(userExamResult.getId());
			examResultService.saveBatch(userExamResult.getExamResults());
			return userExamResult;
		}}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	@Override
	public UserExamResult UpdateUserExamResult(UserExamResult userExamResult) {
		if (this.updateById(userExamResult)) {
			return userExamResult;
		}
		return null;
	}

	@Override
	public UserExamResult GetUserExamResult(Integer id) {
		QueryWrapper<UserExamResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	//父类方法暂不使用
	@Override
	public IPage<UserExamResult> ListUserExamResult(Integer page, Integer count, Boolean withoutBan) {
		return null;
	}

	/**
	 * @param stateFilter 过滤标签 0.只显示未批改  1.只显示已批改  2.不过滤(default)
	 */
	public IPage<Map<String, Object>> ListUserExamResult(Integer page, Integer count,Integer examId,
													Integer stateFilter) {
		IPage<Map<String, Object>> ipage = new Page<>(page, count);
		return baseMapper.getAdminList(ipage,examId,stateFilter);
	}
	
	
	public Integer GetJoinExamStudentCount(Integer id) {
		return baseMapper.getUserJoinCourseExamCount(id);
	}
	@Override
	public IPage<UserExamResult> ListUserExamResultWithUserId(Integer userId, Integer page, Integer count,
			Boolean withoutBan) {
		IPage<UserExamResult> ipage = new Page<>(page, count);
		return baseMapper.getUserList(ipage,userId);
	}

	@Override
	public UserExamResult SetUserExamResultState(Integer id,Integer state) {
		UpdateWrapper<UserExamResult> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	public Boolean CheckDuplicate(Integer examId,Integer studentId){
		return this.getOne(new QueryWrapper<UserExamResult>().eq("exam_id",
				examId).eq("student_id", studentId)) != null;
	}

	public List<LinkedHashMap<String, Object>> GetExamDetails(Integer examId, Integer studentId){
		return baseMapper.getDetails(examId,studentId);
	}

	public int GeOutstandingCount(Integer course) {
		// TODO Auto-generated method stub
		return baseMapper.GeOutstandingCount(course);
	}

	public int GetGoodCount(Integer course) {
		// TODO Auto-generated method stub
		return baseMapper.GetGoodCount(course);
	}

	public IPage<Map<String, Object>> ListExamResult(Integer count, Integer page, Integer course) {
		// TODO Auto-generated method stub.
		IPage<Map<String, Object>> ipage = new Page<>(page, count);
		return baseMapper.ListExamResult(ipage,course);
	}
}
